package br.ufrrj.im.redes.client;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
//import java.net.InetAddress;
import java.net.InetSocketAddress;
//import java.net.NetworkInterface;
import java.net.Socket;
//import java.net.SocketException;
import java.net.UnknownHostException;
//import java.util.Enumeration;

import br.ufrrj.im.redes.model.AuthenticationMessage;
import br.ufrrj.im.redes.model.Message;
import br.ufrrj.im.redes.model.User;
import br.ufrrj.im.redes.util.Constants;

public class Client {

	private Socket requestAuthenticationSocket;
	private String server_IP;
	private User user;
	
	public Client(String server_IP, String name, int mode) throws UnknownHostException, IOException {
		// TODO Auto-generated constructor stub
		this.server_IP = server_IP;
		
		//cria o socket de autenticação para com o servidor
		requestAuthenticationSocket = new Socket();
		
		//define o timeout
		requestAuthenticationSocket.connect(new InetSocketAddress(server_IP, Constants.PORT_REQUEST_CONNECTION), Constants.AUTHENTICATION_TIME_OUT);
		
		//cria o user relacionado ao nome e o endereço de IP do socket
		this.user = new User(name, mode, "");	
	}
	
	/*public static String getMyIP() throws UnknownHostException, SocketException{
		
		String ipAddr = "";
		
	   Enumeration<NetworkInterface> n = NetworkInterface.getNetworkInterfaces();
       for (; n.hasMoreElements();)
       {
               NetworkInterface e = n.nextElement();
               Enumeration<InetAddress> a = e.getInetAddresses();
               
               while(a.hasMoreElements())
               {
                       InetAddress addr = a.nextElement();
                       
                       if(e.getName().equals("wlan0"))
                    	   ipAddr = "\n" + addr.getHostAddress();
               }
       }		

       return ipAddr;
	}*/
	
	public int requestConnectionToServer(){
		
		AuthenticationMessage authenticationMessage;
		AuthenticationMessage authenticationMessageAnswer;
		ObjectOutputStream msgToServer;
		ObjectInputStream msgFromServer;
		
			authenticationMessage = new AuthenticationMessage(user, Constants.AUTHENTICATION_REQUEST);
			
			try {
				
				msgToServer = new ObjectOutputStream(requestAuthenticationSocket.getOutputStream());
				msgToServer.writeObject(authenticationMessage);
				
				msgFromServer = new ObjectInputStream(requestAuthenticationSocket.getInputStream());
				authenticationMessageAnswer = validateAuthenticationMsg(msgFromServer.readObject());
				
				msgToServer.close();
				msgFromServer.close();
				stopConnectionRequestAuthentication();
				
				return authenticationMessageAnswer.getCodeMessage();
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				
				return Constants.AUTHENTICATION_GAME_ERROR;
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				
				return Constants.AUTHENTICATION_GAME_ERROR;
			} 		
	}
	
	public void stopConnectionRequestAuthentication() throws IOException{
		requestAuthenticationSocket.close();
	}
	
	private AuthenticationMessage validateAuthenticationMsg(Object msg){
		
		if (msg instanceof AuthenticationMessage) {
			return (AuthenticationMessage) msg;
		}
		
		return null;
	}

	
	public boolean sendMessageToServer(String ipServer, Message message){
		return true;
	}
	

	public String getServer_IP() {
		return server_IP;
	}

	public void setServer_IP(String server_IP) {
		this.server_IP = server_IP;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	
	
	
	
}
